from numpy import shape,reshape,array
import ga

def test_setChromosomeLength():
    ga.setChromosomeLength('-25,35;16,136;143,1000')
    assert ga.algorithmSettings['chromosomeLength'] == [7,8,10]
    ga.setChromosomeLength('0,130;5,127;-10,-2')
    assert ga.algorithmSettings['chromosomeLength'] == [8,7,5]
    ga.setChromosomeLength('0,130;5,127;-2,-10')
    assert ga.algorithmSettings['chromosomeLength'] == [8,7,5]

def test_initChromosome():
    ga.algorithmSettings['chromosomeLength'] = [7,8,10]
    assert len(ga.initChromosome()) == 25
    ga.algorithmSettings['chromosomeLength'] =[8,7,5]
    assert len(ga.initChromosome()) == 20

def test_initPopulation():
    ga.algorithmSettings['chromosomeLength'] = [10,15]
    ga.algorithmSettings['populationSize'] = 20
    assert shape(ga.initPopulation()) == (20,25)

    ga.algorithmSettings['chromosomeLength'] = [5,3]
    ga.algorithmSettings['populationSize'] = 2
    assert shape(ga.initPopulation()) == (2,8)

def test_crossover():
    x = array(range(1,26))
    population = x.reshape(5,5)
    print "Before Cross Over, the population is "
    print population
    ga.algorithmSettings['crossoverRate'] = 1
    print "After Cross Over, the population is "
    print ga.crossover(population)

def test_mutation():
    population = [[1,0,0,1],[0,0,0,1],[1,1,0,0],[0,0,0,1]]
    print "Before Mutation, the population is "
    print population
    ga.algorithmSettings['mutationRate'] = 1
    print "After Mutation, the population is "
    print ga.mutate(population)

def test_splitMember():
    chromosomeLength = [2,3,4]
    member = [1,0,1,1,0,0,1,1,0]
    assert ga.splitMember(member, chromosomeLength) == ['10','110','0110']

    chromosomeLength = [3,3,3]
    assert ga.splitMember(member, chromosomeLength) == ['101','100','110']

def test_evaluateVariableValues():
    population = [[0,0,0,0,0,0,0,0,0], [1,1,1,1,1,1,1,1,1], [1,0,1,1,0,0,1,1,0]]
    ga.algorithmSettings['chromosomeLength'] = [2,3,4]
    ga.negativeValues = ['No','No','No']
    assert ga.evaluateVariableValues(population) == [[0,0,0],[3,7,15],[2,6,6]]
    
    ga.negativeValues = ['No','Yes','Yes']
    assert ga.evaluateVariableValues(population) == [[0,0,0],[3,-3,-7],[2,-2,6]]

#def test_normalizeFitness():
#    fitnessValues = [10, 20, 30, 50, 100]
#    assert ga.normalizeFitness(fitnessValues) ==

def test_evaluateFitness():
    population = [[0,0,0,0,0,0,0,0,0], [1,1,1,1,1,1,1,1,1], [1,0,1,1,0,0,1,1,0]]
    variables = 'x y z'
    objectiveFunctions = 'x+y+z'
    constraintFunctions = ['x*y<=8']

    ga.algorithmSettings['chromosomeLength'] = [2,3,4]
    ga.negativeValues = ['No','No','No']
    
    assert ga.evaluateFitness(population, objectiveFunctions, constraintFunctions, variables) == [0, 25-10000, 14-10000]

def test_rouletteWheel():
    population = [[0,0,0,0,0,0,0,0,0], [1,1,1,1,1,1,1,1,1], [1,0,1,1,0,0,1,1,0]]
    fitnessValues = [10, 20, -30]
    ga.algorithmSettings['populationSize'] = 3
    print ga.rouletteWheel(population, fitnessValues)

if __name__ == "__main__":
    #test_setChromosomeLength()
    #test_initChromosome()
    #test_initPopulation()
    #test_crossover()
    #test_mutation()
    #test_splitMember()
    #test_evaluateVariableValues()
    #test_evaluateFitness()
    test_rouletteWheel()
    
